package com.mediacloud.app.newsmodule.fragment.ws

import android.graphics.Color
import android.text.TextUtils
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.newsmodule.R
import com.tencent.mmkv.MMKV
import com.utils.DisplayUtil
import java.text.SimpleDateFormat
import java.util.*

class WS24ListAdapter : BaseQuickAdapter<TimeLineItem, BaseViewHolder>(R.layout.item_ws24_2) {

    val sdf0 = SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA)
    val sdf1 = SimpleDateFormat("HH:mm", Locale.CHINA)
    val sdf2 = SimpleDateFormat("M月d日 HH:mm", Locale.CHINA)

    override fun convert(helper: BaseViewHolder, item: TimeLineItem?) {
        val title = helper.getView<TextView>(R.id.title)
        title.text = item?.title
        val logo = helper.getView<ImageView>(R.id.logo)
        val iv_play_tag = helper.getView<ImageView>(R.id.iv_play_tag)
        val tvNewsTipV2 = helper.getView<TextView>(R.id.tvNewsTipV2)

        logo.loadX(item?.logo, 1,1,false)
        val time = helper.getView<TextView>(R.id.time)
        val timeInner = helper.getView<TextView>(R.id.timeInner)
        val v0 = helper.getView<View>(R.id.v0)

        if (helper.absoluteAdapterPosition == 0) {
            v0.visibility = View.GONE
        } else {
            v0.visibility = View.VISIBLE
        }


        /*title.post {
            val layoutParams = timeInner.layoutParams as ConstraintLayout.LayoutParams
            if (title.lineCount < 3) {
                layoutParams.bottomToBottom = R.id.logo
                layoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET
                layoutParams.topMargin = 0
            } else if (title.lineCount == 3) {
                layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
                layoutParams.topToBottom = R.id.logo
                layoutParams.topMargin = 0
            } else {
                layoutParams.bottomToBottom = ConstraintLayout.LayoutParams.UNSET
                layoutParams.topToBottom = R.id.title
                layoutParams.topMargin = DisplayUtil.dip2px(mContext, 12f)
            }
            timeInner.layoutParams=layoutParams
        }
*/
        try {
            item?.let {
                val timeLong = sdf0.parse(item.publishdate)?.time ?: 0
                time.text = sdf1.format(timeLong)
                timeInner.text = sdf2.format(timeLong)

                if (MMKV.defaultMMKV().getBoolean(item.id.toString(), false)) {
                    title.setTextColor(Color.parseColor("#666666"))
                } else {
                    title.setTextColor(Color.parseColor("#222222"))
                }

                if (item.type==5){
                    iv_play_tag.visibility = View.VISIBLE
                    tvNewsTipV2.visibility = View.VISIBLE
                    if(!TextUtils.isEmpty(item.prop4)){
                        tvNewsTipV2.text = item.prop4
                    }else{
                        tvNewsTipV2.text = "00:00"
                    }
                }else{
                    iv_play_tag.visibility = View.GONE
                    tvNewsTipV2.visibility = View.GONE
                }
            }

        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}